package github.kawaiior.botapiserver.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import github.kawaiior.botapiserver.common.ActionResponse;
import github.kawaiior.botapiserver.common.DataResponse;
import github.kawaiior.botapiserver.entity.Bot;
import github.kawaiior.botapiserver.entity.BotGroup;
import github.kawaiior.botapiserver.entity.GroupUser;
import github.kawaiior.botapiserver.entity.Ip;
import github.kawaiior.botapiserver.mapper.BotMapper;
import github.kawaiior.botapiserver.mapper.GroupMapper;
import github.kawaiior.botapiserver.mapper.GroupUserMapper;
import github.kawaiior.botapiserver.mapper.IpMapper;
import github.kawaiior.botapiserver.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BotService {

    private final Logger logger = LoggerFactory.getLogger(BotService.class);

    @Autowired
    private BotMapper botMapper;

    @Autowired
    private IpMapper ipMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private GroupUserMapper groupUserMapper;

    private static final ActionResponse BOT_EXIST = new ActionResponse(0, "BOT已存在");

    public ActionResponse initBot(String botId, String name, String intro) {
        if (botMapper.selectOne(new QueryWrapper<Bot>().eq("bot_id", botId))!=null){
            return BOT_EXIST;
        }
        botMapper.insert(new Bot(botId, name, intro));
        return ActionResponse.DEFAULT_SUCCESS_RESPONSE;
    }

    private static final ActionResponse GROUP_EXIST = new ActionResponse(0, "群组已存在");

    public ActionResponse initBotGroup(String botId, String groupId, String name) {
        if (groupMapper.selectOne(new QueryWrapper<BotGroup>()
                .eq("bot_id", botId)
                .eq("group_id", groupId))!=null){
            return GROUP_EXIST;
        }
        groupMapper.insert(new BotGroup(name, groupId, botId));
        return ActionResponse.DEFAULT_SUCCESS_RESPONSE;
    }

    private final static ActionResponse GROUP_USER_EXIST = new ActionResponse(0, "用户已注册");

    public ActionResponse initGroupUser(String botId, String groupId, String userId, String name) {
        if (groupUserMapper.selectOne(new QueryWrapper<GroupUser>()
                .eq("bot_id", botId)
                .eq("group_id", groupId)
                .eq("user_id", userId))!=null){
            return GROUP_USER_EXIST;
        }
        groupUserMapper.insert(new GroupUser(name, groupId, botId, userId));
        return ActionResponse.DEFAULT_SUCCESS_RESPONSE;
    }

    private static final ActionResponse BOT_NOT_EXIST = new ActionResponse(0, "BOT不存在");

    public ActionResponse getInfo(String botId) {
        Bot bot = botMapper.selectOne(new QueryWrapper<Bot>().eq("bot_id", botId));
        if (bot==null){
            return BOT_NOT_EXIST;
        }
        return new DataResponse<>(bot);
    }

    private static final ActionResponse NOT_IP_STR = new ActionResponse(0, "非法IP");
    private static final ActionResponse NOT_ALLOW = new ActionResponse(0, "权限不足");
    private static final ActionResponse ALLOW = new ActionResponse(1, "允许请求");
    public boolean checkIp(String botId, String groupId, String userId, String ip){
        // 判断ip
        if (ipMapper.selectOne(new QueryWrapper<Ip>()
                .eq("bot_id", botId)
                .eq("group_id", groupId)
                .eq("user_id", userId)
                .eq("ip", ip))!=null){
            return true;
        }
        // 判断user ip
        if (ipMapper.selectOne(new QueryWrapper<Ip>()
                .eq("bot_id", botId)
                .eq("group_id", "-1")
                .eq("user_id", userId)
                .eq("ip", ip))!=null){
            return true;
        }
        // 判断group ip
        if (ipMapper.selectOne(new QueryWrapper<Ip>()
                .eq("bot_id", botId)
                .eq("group_id", groupId)
                .eq("user_id", "-1")
                .eq("ip", ip))!=null){
            return true;
        }
        return false;
    }

    public ActionResponse checkAccess(String botId, String groupId, String userId, String ip) {
        if (!StringUtil.isIPAddress(ip)){
            return NOT_IP_STR;
        }
        if (checkIp(botId, groupId, userId, ip)){
            logger.info("BOT_ID: {} - GROUP_ID: {} - USER_ID: {} - IP: {} - ALLOW", botId, groupId, userId, ip);
            return ALLOW;
        }
        logger.info("BOT_ID: {} - GROUP_ID: {} - USER_ID: {} - IP: {} - NOT ALLOW !", botId, groupId, userId, ip);
        return NOT_ALLOW;
    }

}
